#!/usr/bin/python

"""test1.py

this script is
"""

__author__ = 'takahiro'
__version__ = '2014-02-10 1.00 (takahiro)'

import sys
import optparse

usage = "%prog [filename] <UPDOWN(1: UP, 0:DOWN)> <NUM>"
parser = optparse.OptionParser( usage=usage )
parser.parse_args( )

SIGNAL_OFF = 0
SIGNAL_ON = 1

UP = 1
DOWN = 0

if len( sys.argv ) == 3:
  stream = sys.stdin
  UPDOWN = int(sys.argv[1])
  NUM = int(sys.argv[2])
elif len( sys.argv ) == 4:
  stream = open( sys.argv[ 1 ], 'r' )
  UPDOWN = int(sys.argv[2])
  NUM = int(sys.argv[3])
else:
  parser.print_help( )
  exit( )

if NUM <= 0 or (UPDOWN != UP and UPDOWN != DOWN):
  parser.print_help( )
  exit( )

keys = open('format.arg', 'r').readlines()[0].rstrip().split(' ')

datas = []

for line in stream:
  array = line.rstrip().split('\t')
  bw = int(array[keys.index('bw')])
  datas.append(bw)
  while len(datas) > NUM:
    datas.pop(0)
  if len(datas) < NUM:
    print SIGNAL_OFF
    continue
  flag = SIGNAL_ON
  for val in datas:
    if UPDOWN == UP and val < 0:
      flag = SIGNAL_OFF
    elif UPDOWN == DOWN and val > 0:
      flag = SIGNAL_OFF
  print flag 
  
